<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/common/session.jsp"%>
<c:set var="deptId" value="${param.deptId}" />
<c:set var="deptParentId" value="${param.deptParentId}" />
<c:set var="staffId" value="${param.staffId}" />
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <title>员工管理</title>
    <link rel="stylesheet" href="${ctx}/css/xtable.css"/>
    <script type="text/javascript" src="${ctx}/js/calendar/WdatePicker.js"></script>
    <script type="text/javascript" src="${ctx}/js/validator.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/UserService.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/DeptService.js"></script>
</head>
<body style="text-align: center;">
<div style="width: 700px; margin:0 auto">
    <form id="staffForm" name="staffForm" method="post">
        <input type="hidden" id="userId" name="userId" value="">
        <input type="hidden" id="staffId" name="staffId" value="${staffId}">
        <table cellpadding="15" cellspacing="0" width="100%" border="1">
            <tr>
                <td>
                    信息部
                </td>
                <td>
                    <select id="deptId" name="deptId">
                    </select>
                </td>
            </tr>
            <tr>
                <td width="200">
                    员工姓名
                </td>
                <td>
                    <input type="text" id="userName" name="userName" style="width: 300px" required>
                </td>
            </tr>
            <tr>
                <td>
                    手机号
                </td>
                <td>
                    <input type="text" id="userMobile" name="userMobile" maxlength="11" style="width: 300px" onblur="selectUserList()" required>
                </td>
            </tr>
            <tr>
                <td>
                    入职时间
                </td>
                <td>
                    <input type="text" id="staffEntryDate" name="staffEntryDate" style="width: 300px" onfocus="WdatePicker()" required>
                </td>
            </tr>
            <tr>
                <td>
                    员工状态
                </td>
                <td>
                    <select id="staffState" name="staffState">
                        <option value="0">试用</option>
                        <option value="1">在职</option>
                        <option value="2">离职</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    是否联系人
                </td>
                <td>
                    <select id="staffType" name="staffType">
                        <option value="0">否</option>
                        <option value="1">是</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    员工照片
                </td>
                <td>
                    <input type="file" id="staffPhotoFile" name="staffPhotoFile" accept="image/jpeg,image/x-png" style="width: 300px">
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <img src="${ctx}/images/common/button_save.jpg" title="保存" style="cursor:pointer" onclick="insertStaff()"/>
                    <img src="${ctx}/images/common/button_back.jpg" title="返回" style="cursor:pointer" onclick="callback()"/>
                </td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>
<script type="text/javascript">
    var dept = new Dept();
    DeptService.selectDeptList(dept, callbackSelectDeptList);
    function callbackSelectDeptList(list) {
        dwr.util.addOptions("deptId", list, "deptId", "deptName");
        dwr.util.setValue("deptId", "${deptId}");
        if("${staffId}".length > 0){
            var staff = new Staff();
            staff.staffId = "${staffId}";
            UserService.selectStaffList(staff, callbackSelectStaffList);
        }
    }
    function callbackSelectStaffList(list) {
        var staff = list[0];
        setModelToForm(staff);
        dwr.util.setValue("staffEntryDate", new Date(staff.staffEntryDate).Format("yyyy/MM/dd"));
    }
    function selectUserList(){
        var user = new Uesr();
        user.userMobile = dwr.util.getValue("userPhone");
        UserService.selectUserList(user);
    }
    function insertStaff(){
        if(validate($("staffForm"))){
            var staff = dwr.util.getFormValues("staffForm");
            staff.staffEntryDate = new Date(dwr.util.getValue("staffEntryDate"));
            var user = dwr.util.getFormValues("staffForm");
            user.userType = 1;
            user.userState = 1;
            staff.user = user;
            var length = $("staffPhotoFile").files.length;
            if(length > 0) {
                var oFReader = new FileReader();
                oFReader.readAsDataURL($("staffPhotoFile").files[0]);
                oFReader.onload = function (oFREvent) {
                    var base64 = oFREvent.target.result.split(",")[1];
                    staff.staffPhoto = base64;
                    UserService.insertStaff(staff, "${deptParentId}", callback);
                }
            } else {
                UserService.insertStaff(staff, "${deptParentId}", callback);
            }
        }
    }
    function callback() {
        location = "${ctx}/user/staff_list.jsp?deptId=${deptId}&deptParentId=${deptParentId}";
    }
</script>